/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.caigourenyuan;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingmen.canting.R;
import com.mingmen.canting.activity.RegisterActivity;
import com.mingmen.canting.adapter.FileerFangqiAdapter;
import com.mingmen.canting.adapter.MultipleXianXiaRecyclerAdapter;
import com.mingmen.canting.label.adapter.MultipleRecyclerAdapter;
import com.mingmen.canting.label.bean.LabelTestBean;
import com.mingmen.canting.label.recycler.OnItemClickListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class XianXiaZiCaiActivity extends Activity {
    @BindView(R.id.img_return)
    ImageView imgReturn;

    @BindView(R.id.layout_close)
    LinearLayout layoutClose;
    @BindView(R.id.text_1)
    TextView text1;
    @BindView(R.id.text_2)
    TextView text2;
    @BindView(R.id.text_3)
    TextView text3;

    String string;
    String attrs = "";
    @BindView(R.id.btn_select)
    Button btn_select;
    String count;
    List<String> dingdan = new ArrayList<>();

    @BindView(R.id.main_recycler)
    RecyclerView main_recycler;
    int myposition = 0;
    String kuaijie;
    @BindView(R.id.btn_cancel)
    Button btn_cancel;
    private List<LabelTestBean> mListData;
    private MultipleXianXiaRecyclerAdapter mAdapter_label;
    String applyId_s;
    String commpanyNames;
    List<String> idSend = new ArrayList<>();
    private int resurtResurant = 200;

    List<String> stringList = new ArrayList<>();
    List<String> commpanyList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__filterxianxia);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        dingdan.add("供应商无货");
        dingdan.add("其他");
      /*  GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        main_recycler.setLayoutManager(gridLayoutManager);*/
        applyId_s = getIntent().getStringExtra("applyId_s");
        commpanyNames = getIntent().getStringExtra("commpanyNames");
        commpanyNames=commpanyNames.substring(1,commpanyNames.length()-1);

        Log.d("XianXiaZiapplyId_s", applyId_s);
        Log.d("XianXiaZicommpanyNames", commpanyNames);

        main_recycler.setLayoutManager(new LinearLayoutManager(this));

        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.custom_divider));

        main_recycler.addItemDecoration(divider);
        String str[] = applyId_s.split(",");
        stringList = Arrays.asList(str);



//        commpanyList
        String company[]=commpanyNames.split(",");
//        commpanyList= Arrays.asList(company);

        for (int i=0;i<commpanyList.size();i++){
            Log.d("XianXiaZi", commpanyList.get(i));

        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        main_recycler.setLayoutManager(gridLayoutManager);
        mListData = new ArrayList<>();
        mAdapter_label = new MultipleXianXiaRecyclerAdapter(this, mListData);
        main_recycler.setAdapter(mAdapter_label);

        for (int i = 0; i < stringList.size(); i++) {
//            mListData.add(new LabelTestBean(stringList.get(i), commpanyList.get(i)));

            mListData.add(new LabelTestBean(stringList.get(i), company[i]));
        }
        for (int i = 0; i < mListData.size(); i++) {
            Log.d("SELECTGONGYING", mListData.get(i).getTagId());
        }
        mAdapter_label.update(mListData);
        mAdapter_label.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                addOrRemove(position);
            }

            @Override
            public void OnItemLongClick(View view, int position) {

            }
        });
    }


    @OnClick({R.id.img_return, R.id.layout_close, R.id.text_1, R.id.text_2, R.id.text_3, R.id.btn_select, R.id.btn_cancel})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {

            case R.id.img_return:
            case R.id.layout_close:
                finish();
                break;
            case R.id.text_1:
                text1.setBackground(getResources().getDrawable(R.drawable.shape_label_select));
                text1.setTextColor(Color.parseColor("#BF0B22"));
                text2.setBackground(getResources().getDrawable(R.drawable.shape_label_unselect));
                text2.setTextColor(Color.parseColor("#303030"));
                text3.setBackground(getResources().getDrawable(R.drawable.shape_label_unselect));
                text3.setTextColor(Color.parseColor("#303030"));
                break;
            case R.id.text_2:
                text2.setBackground(getResources().getDrawable(R.drawable.shape_label_select));
                text2.setTextColor(Color.parseColor("#BF0B22"));
                text1.setBackground(getResources().getDrawable(R.drawable.shape_label_unselect));
                text1.setTextColor(Color.parseColor("#303030"));
                text3.setBackground(getResources().getDrawable(R.drawable.shape_label_unselect));
                text3.setTextColor(Color.parseColor("#303030"));
                break;
            case R.id.text_3:
                text3.setBackground(getResources().getDrawable(R.drawable.shape_label_select));
                text3.setTextColor(Color.parseColor("#BF0B22"));
                text2.setBackground(getResources().getDrawable(R.drawable.shape_label_unselect));
                text2.setTextColor(Color.parseColor("#303030"));
                text1.setBackground(getResources().getDrawable(R.drawable.shape_label_unselect));
                text1.setTextColor(Color.parseColor("#303030"));
                break;
            case R.id.btn_select:
                intent = new Intent(XianXiaZiCaiActivity.this, SelectGongYingActivity.class);
                intent.putExtra("idSend", idSend.toString());
                setResult(200, intent);
                finish();
                Log.d("idSend", idSend.toString());

                break;
            case R.id.btn_cancel:
                finish();
                break;
        }
    }

    public void onEventMainThread(Integer type) {

    }

    public static Set<Integer> positionSet = new HashSet<>();
    public Set<String> checkTYpeNameSet = new HashSet<>();
    private boolean selectMode = true; //选择模式 多选或者单选  true  多选
    private String checktypeId = ""; //记录选择种类 多种类以" ,"分开

    private void addOrRemove(int position) {
        if (positionSet.contains(position)) {
            // 如果包含，则撤销选择
            positionSet.remove(position);
//            checkTYpeNameSet.remove(mListData.get(position).getTagName());
            checkTYpeNameSet.remove(mListData.get(position).getTagId());
        } else {
            // 如果不包含，则添加
            positionSet.add(position);
//            checkTYpeNameSet.add(mListData.get(position).getTagName());
            checkTYpeNameSet.add(mListData.get(position).getTagId());
        }

        if (positionSet.size() == 0) {
            // 如果没有选中任何的item，则退出多选模式
            mAdapter_label.notifyDataSetChanged();
            selectMode = false;
        } else {
            // 更新列表界面，否则无法显示已选的item
            mAdapter_label.notifyDataSetChanged();
        }
        Log.e("info", positionSet.toString());
        if (idSend.size() > 0) {
            idSend.clear();
        }
//        idSend=Arrays.asList(checkTYpeNameSet.toString());
//        List<String> result = new ArrayList<>(checkTYpeNameSet);

        idSend = new ArrayList<>(checkTYpeNameSet);
        Log.e("ArraysArraysArrays", idSend.toString());
//        idSend.add(result.toString());
//        Toast.makeText(SelectGongYingActivity.this, checkTYpeNameSet.toString(), Toast.LENGTH_SHORT).show();
    }
}